package com.dz.lm.web.controller.newuser.useragent;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.useragent.UserAgent;
import com.dz.lm.service.useragent.UserAgentService;
import com.dz.lm.service.userproduct.UserProductService;
import com.dz.lm.web.controller.BasicControl;
import com.smartlbs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/** 代理控制层
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/13 21:11;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userAgent", produces = "application/json")
public class UserAgentController extends BasicControl {
    @Autowired
    private UserAgentService userAgentService;
    @Autowired
    private UserProductService userProductService;
    /**
     * 添加代理
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        Map<String, Object> images = (Map<String, Object>) request.getAttribute("images");
        Map<String, Object> urlMap = new HashMap<String, Object>();
        Object imgUrl = request.getAttribute("imgUrl");

        if (images != null && !images.isEmpty()) {
            urlMap = FileUploadUtil.uploadByInStream(images, imgUrl, "apply");
        }
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        StringBuilder urlsbulidr = new StringBuilder();
        if (images != null && !images.isEmpty()) {
            for (String key : images.keySet()) {
                urlsbulidr.append(imgUrl + urlMap.get(key).toString() + ",");
            }
        }

        String urls = urlsbulidr.toString();
        //用户
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //原生产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 0;
        //代理返点设置
        double returnPoint = param.get("returnPoint") != null ? StringUtil.getDouble(param.get("returnPoint").toString(),-1): -1;
        //代理折扣
        double agentSale = param.get("agentSale") != null ? StringUtil.getDouble(param.get("agentSale").toString(),-1): -1;
        //代理级别
        String levelNum = param.get("levelNum") != null ? param.get("levelNum").toString() : "";
        //模板json
        String template = param.get("template") != null ? param.get("template").toString() : "";
        if (userId == 0 || userId == 0 || productId == 0 ) {
            message="参数有误";
            result= "02";
        }else {
            Map retMap = userAgentService.add(userId, productId,false,param.get("platform").toString(),true,template,urls,returnPoint,agentSale,levelNum);
            int i = StringUtil.getInt(retMap.get("ret"), 0);
            if (i > 0) {
                Map map = new HashMap();
                map.put("id",i);
                if(retMap.get("reviewId") != null){
                    map.put("reviewId",StringUtil.getInt(retMap.get("reviewId"),0));
                }
                data =map;
                message="操作成功";
                result= "00";
            }else if(i ==-997){
                result="02";
                message="已代理过该产品";
            }else if(i == -996){
                result ="02";
                message="产品已删除";
            }else if(i == -998){
                result = "02";
                message="代理已被拒绝30内无法再次代理";

            }else if(i == -995){
                result = "02";
                message="已申请代理，审核中";
            }
        }
        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }
    /**
     * 取消代理
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/cancelAgent")
    public Map cancelAgent(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //登录人
        String uId = request.getAttribute("userId")!=null?request.getAttribute("userId").toString():"";

        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //代理id
        Integer agentId = param.get("agentId") != null ? Integer.parseInt(param.get("agentId").toString()) : 0;
        //代理人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;

        if (agentId == 0 ) {
            message="参数有误";
            result= "02";
        }else {
            int i = userAgentService.cancelAgent(Integer.parseInt(uId),userId,agentId);
            if (i > 0) {
                message="操作成功";
                result= "00";
            }else if(i==-998){
                result="02";
                message="有相关申请无法取消，无法取消";
            }
        }
        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }
    /**
     * 获取代理列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId")!=null?request.getAttribute("userId").toString():"";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //受理人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 0;
        //staus 1正常代理 2取消代理
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 1;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = userAgentService.query(pageNum+1, rows, userId,productId,status);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
}
